package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EchoClient extends JFrame implements Runnable{
	
	InputStream is = null;
	InputStreamReader isr = null;
	BufferedReader br = null;
	
	BufferedWriter bw = null;
	OutputStream os = null;
	OutputStreamWriter osw = null;
	boolean threadFlag = true;
	
	StringBuilder data = new StringBuilder();

	private JPanel contentPane;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JTextField serverIP;
	private JLabel lblNewLabel_1;
	private JTextField port;
	private JButton btnConnect;
	private JButton btnDisconnect;
	private JScrollPane scrollPane;
	private JTextPane result;
	private JPanel panel_1;
	private JButton btnNewButton;
	private JTextField message;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					EchoClient frame = new EchoClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EchoClient() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 539, 487);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
	}
	
	@Override
	public void run() {
		btnConnect.setEnabled(false);
		btnDisconnect.setEnabled(true);
		
		String ip = serverIP.getText();
		int p = Integer.parseInt(port.getText());
		try{
			Socket s = new Socket(ip,p);

			os = s.getOutputStream();
			osw = new OutputStreamWriter(os);
			bw = new BufferedWriter(osw);

			is = s.getInputStream();
			isr = new InputStreamReader(is);
			br = new BufferedReader(isr);
			data.append("서버에 접속되었습니다.<br/>");
			result.setText(data.toString());
			
			while(threadFlag){
				String str = br.readLine();
				data.append(str + "<br/>");
				result.setText(data.toString());
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void send(String str){
		try{
			bw.write(str);
			bw.newLine();
			bw.flush();
			data.append(str + "<br/>");
			result.setText(data.toString());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.add(getLblNewLabel());
			panel.add(getServerIP());
			panel.add(getLblNewLabel_1());
			panel.add(getPort());
			panel.add(getBtnConnect());
			panel.add(getBtnDisconnect());
		}
		return panel;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("서버주소");
		}
		return lblNewLabel;
	}
	private JTextField getServerIP() {
		if (serverIP == null) {
			serverIP = new JTextField();
			serverIP.setColumns(10);
			
			try{
				InetAddress ia = InetAddress.getLocalHost();
				serverIP.setText(ia.getHostAddress());
				setTitle(ia.getHostAddress());
			}catch(Exception ex){
				ex.printStackTrace();
			}

		}
		return serverIP;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Port");
		}
		return lblNewLabel_1;
	}
	private JTextField getPort() {
		if (port == null) {
			port = new JTextField();
			port.setText("4444");
			port.setColumns(10);
		}
		return port;
	}
	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton("서버접속");
			btnConnect.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					threadFlag = true;
					Thread t = new Thread(EchoClient.this);
					t.start();
						
					
				}
			});
		}
		return btnConnect;
	}
	private JButton getBtnDisconnect() {
		if (btnDisconnect == null) {
			btnDisconnect = new JButton("접속해제");
			btnDisconnect.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					btnConnect.setEnabled(true);
					btnDisconnect.setEnabled(false);
					try{
						if(bw != null) bw.close();
					}catch(Exception ex){}
				}
			});
		}
		return btnDisconnect;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}


	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getBtnNewButton(), BorderLayout.EAST);
			panel_1.add(getMessage(), BorderLayout.CENTER);
		}
		return panel_1;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("전송");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					send(message.getText());
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getMessage() {
		if (message == null) {
			message = new JTextField();
			message.setColumns(10);
		}
		return message;
	}
}
